/**
 * chooseCourse
 *
 * @author LongShu 2017/06/12
 */
(function () {
    var course_table = null;

    var chooseCourseApp = new Vue({
        el: '#chooseCourse',
        data: {
            selectCourseCategory: '0',
            courseCategoryList: [],
            courseList: [],
            course: {},
            msg: null,
            showList: false,
            refreshing: false
        },
        methods: {
            chooseCourseCategory: function () {
                if (this.selectCourseCategory !== '0') {
                    this.showCourseList();
                }
            },
            showCourseList: function () {
                if (this.selectSemester === '0') {
                    this.showMsg("请选择课程类别!");
                    return;
                }
                if (this.refreshing) {
                    return;
                }
                this.refreshing = true;

                this.$http.get(fixUrl("chooseCourse/courseInfo?id=" + this.selectCourseCategory)).then(function (response) {
                    $('.alert').hide();
                    var webData = new WebData(response.body);
                    if (webData.isError()) {
                        this.showMsg(webData.msg);
                        return false;
                    }

                    this.refreshing = false;
                    this.courseList = webData.data;

                    if (webData.data.length < 1) {
                        this.showMsg("哎呀,好像没有数据哎!");
                        this.showList = false;
                        return false;
                    }
                    this.showList = true;
                    this.showTable(this.courseList);
                }, function (response) {
                    console.warn(response.body);
                    this.showMsg("获取课程列表失败!");
                });
            },
            chooseCourse: function () {
                this.$http.post(fixUrl("chooseCourse/chooseCourse"), this.course).then(function (response) {
                    var webData = new WebData(response.body);

                    var start = webData.msg.indexOf("<font");
                    var msg = webData.msg.substring(start);
                    start = msg.indexOf(">") + 1;
                    var end = msg.indexOf("</font>");
                    msg = msg.substring(start, end);
                    this.showMsg(msg);
                }, function (response) {
                    console.warn(response.body);
                    this.showMsg("系统不鸟你了,选课失败!");
                });
            },
            showTable: function (data) {
                if (course_table) {
                    course_table.destroy();
                }
                course_table = $(".table").DataTable({
                    lengthMenu: [[10, 20, 50, 100, -1], [10, 20, 50, 100, "所有"]],
                    language: {url: fixUrl("assets/dataTable.zh_CN.json")},
                    data: data,
                    columns: [
                        {data: "RkLs"},
                        {data: "Jxb"},
                        {data: "Sksj"},
                        {data: "MaxRs"},
                        {
                            data: null,
                            render: function (data, type, row, meta) {
                                if (data.Xkrl < 0) {
                                    return '<span class="label label-danger">' + data.Xkrl + '</span>';
                                } else if (data.Xkrl < 20) {
                                    return '<span class="label label-warning">' + data.Xkrl + '</span>';
                                }
                                return data.Xkrl;
                            }
                        },
                        {data: "Zxf"},
                        {// 操作
                            data: null,
                            render: function (data) {
                                if (1 == data.XkZt) {
                                    return "无";
                                }
                                return "<a href='javascript:;' class='btn btn-info btn-small' onclick='doChoose(this)'>选择</a>";
                            }
                        }
                    ]
                });
            },
            showMsg: function (msg) {
                this.msg = msg;
                $('.alert').show();
            }
        }
    });

    Vue.http.get(fixUrl("commonData/courseCategory")).then(function (response) {
        var webData = new WebData(response.body);
        if (webData.isError()) {
            alert(webData.msg);
            return;
        }
        chooseCourseApp.courseCategoryList = webData.data;
    }, function (response) {
        console.warn(response.body);
        alert("获取课程类别失败!");
    });

    window.doChoose = function (opt) {
        chooseCourseApp.course = course_table.row($(opt).parent().parent()).data();
        console.log(chooseCourseApp.course);
        chooseCourseApp.chooseCourse();
    };

})();
